package name.ekr.edu.threads.chatserver.client;


import java.io.IOException;
import java.net.Socket;

import name.ekr.edu.threads.chatserver.ConnectionError;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Connector {
	private static Logger logger = LoggerFactory.getLogger(Connector.class);
	private Socket connection;
	private String host;
	private int port;
	
	public Connector(String host, int port) throws ConnectionError {
		try {
			connection = new Socket(host, port);
			logger.info("Connected to " + host + ":" + port);
			this.host = host;
			this.port = port;
		} catch (IOException e) {
			logger.error("", e);
			throw new ConnectionError("Can't connect to " + host + ":" + port, e);
		} 
	}

	public String getHost() {
		return host;
	}

	public int getPort() {
		return port;
	}

	public Socket getSocket() {
		return connection;
	}
	
	public void close() throws ConnectionError {
		try {
			connection.close();
			logger.info("Closed connection");
		} catch (IOException e) {
			logger.error("", e);
			throw new ConnectionError("Can't close connection", e);
		}
	}

}
